let listLoad = require('../../common/mixinList')
import { formatTime } from '../../utils/util'
Page({
    behaviors: [listLoad],
    data: {
        cate_id: '',
        cateInfo: null,
        reqUrl: '/api/index/rateByProduct',
        usePrivateMethod: true,
        cateList: [],
        currentCateValue: 0,
    },
    onLoad(options) {
        this.data.cate_id = options.cate_id
        options.cate_id !== '0' && this.getCate()
        this.getList(true)
        this.getAllCate()
    },
    getParamsData() {
        return { limit: 20, cate_id: this.data.cate_id }
    },
    privateMethod(refresh, res) {
        return new Promise(resolve => {
            let list = res.data
            list.forEach(item => {
                if (Number(item.change_rate) === 0) {
                    item.isUp = false
                    item.upAndDownPrice = 0
                    item.change_rate = '-' + item.change_rate
                } else {
                    let upAndDownPrice = Number((item.today_price - Number(item.yesterday_price)).toFixed(2))
                    item.isUp = upAndDownPrice > 0
                    item.upAndDownPrice = Math.abs(upAndDownPrice)
                }
            })
            resolve(list)


            if (refresh) {
                this.delay(() => {
                    this.setData({ refresh: false })
                }, 400)
            }
        })
    },
    //获取该cate_id的分类涨跌
    getCate() {
        this.request({
            url: `/api/index/rateCate?cate_id=${this.data.cate_id}`,
            login: false,
        }, res => {
            if (!res.data) return
            if (Number(res.data.change_rate) === 0) {
                res.data.isUp = false
                res.data.upAndDownPrice = 0
                res.data.change_rate = '-' + res.data.change_rate
            } else {
                let upAndDownPrice = Number((res.data.today_price - Number(res.data.yesterday_price)).toFixed(2))
                res.data.isUp = upAndDownPrice > 0
                res.data.upAndDownPrice = Math.abs(upAndDownPrice)
            }

            this.setData({ cateInfo: res.data })
        })
    },
    toEcharts({ mark }) {
        if (mark.index == undefined && !mark.isCate) return
        const { cateInfo, list } = this.data
        let title = mark.isCate ? cateInfo.cate_title : list[mark.index].product_name
        let cate_id = mark.isCate ? cateInfo.cate_id : list[mark.index].cate_id
        let product_common_id = mark.isCate ? '' : list[mark.index].product_common_id
        wx.navigateTo({
            url: `/pages/conjuncture_echarts/index?title=${title}&cate_id=${cate_id}&product_common_id=${product_common_id}`
        })
    },
    //获取全部分类
    getAllCate() {
        this.request({
            url: '/api/product/cate',
            login: false
        }, res => {
            res.data.unshift({
                cate_id: '',
                title: '全部'
            })
            let cateList = res.data.map((item, index) => {
                return {
                    value: index,
                    cate_id: item.cate_id,
                    text: item.title
                }
            })
            this.setData({
                cateList
            })

            if (this.data.cate_id !== 0) this.verificationCateInfo()
        })
    },
    verificationCateInfo() {
        if (this.data.cateInfo == null) {
            this.delay(() => { this.verificationCateInfo() })
        } else {
            this.classification()
        }
    },
    //匹配分类
    classification() {
        const cate_title = this.data.cateInfo.cate_title
        const index = this.data.cateList.findIndex(item => {
            return item.text === cate_title
        })

        if (index !== -1) this.setData({ currentCateValue: index })
    },
    onCateChange(e) {
        this.setData({
            currentCateValue: e.detail
        })
        this.data.cate_id = this.data.cateList[e.detail].cate_id

        e.detail !== 0 ? this.getCate() : this.setData({ cateInfo: null })
        this.getList(true)
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 0
        })
    },
    dropdownMenuToggle() {
        let dropdownMenu = this.selectComponent('#dropdown_menu')
        dropdownMenu.toggle()
    },
    chooseCate({ mark }) {
        const { index } = mark
        if (index == undefined) return
        if (index === this.data.currentCateValue) return
        this.dropdownMenuToggle()
        this.onCateChange({ detail: index })
    },
});